package test01;

// 采购请求类
class PurchaseRequest {
    private double amount;
    private String purpose;

    public PurchaseRequest(double amount, String purpose) {
        this.amount = amount;
        this.purpose = purpose;
    }

    public double getAmount() {
        return amount;
    }

    public String getPurpose() {
        return purpose;
    }
}

// 抽象审批者
abstract class Approver {
    protected Approver successor; // 后继审批者
    protected String name; // 审批者姓名
    protected double minAmount; // 最小审批金额
    protected double maxAmount; // 最大审批金额

    public Approver(String name, double minAmount, double maxAmount) {
        this.name = name;
        this.minAmount = minAmount;
        this.maxAmount = maxAmount;
    }

    // 设置后继审批者
    public void setSuccessor(Approver successor) {
        this.successor = successor;
    }

    // 处理审批请求
    public abstract void processRequest(PurchaseRequest request);
}

// 主任审批者
class Director extends Approver {
    public Director(String name) {
        super(name, 0, 50000);
    }

    @Override
    public void processRequest(PurchaseRequest request) {
        if (request.getAmount() < maxAmount) {
            System.out.println("主任 " + name + " 审批了采购单：" +
                    request.getPurpose() + "，金额：" + request.getAmount() + "元");
        } else if (successor != null) {
            successor.processRequest(request);
        }
    }
}

// 副董事长审批者
class VicePresident extends Approver {
    public VicePresident(String name) {
        super(name, 50000, 100000);
    }

    @Override
    public void processRequest(PurchaseRequest request) {
        if (request.getAmount() >= minAmount && request.getAmount() < maxAmount) {
            System.out.println("副董事长 " + name + " 审批了采购单：" +
                    request.getPurpose() + "，金额：" + request.getAmount() + "元");
        } else if (successor != null) {
            successor.processRequest(request);
        }
    }
}

// 董事长审批者
class President extends Approver {
    public President(String name) {
        super(name, 100000, 500000);
    }

    @Override
    public void processRequest(PurchaseRequest request) {
        if (request.getAmount() >= minAmount && request.getAmount() < maxAmount) {
            System.out.println("董事长 " + name + " 审批了采购单：" +
                    request.getPurpose() + "，金额：" + request.getAmount() + "元");
        } else if (successor != null) {
            successor.processRequest(request);
        }
    }
}

// 董事会审批者
class BoardOfDirectors extends Approver {
    public BoardOfDirectors(String name) {
        super(name, 500000, Double.MAX_VALUE);
    }

    @Override
    public void processRequest(PurchaseRequest request) {
        if (request.getAmount() >= minAmount) {
            System.out.println("董事会 " + name + " 审批了采购单：" +
                    request.getPurpose() + "，金额：" + request.getAmount() + "元");
            System.out.println("需要召开董事会讨论决定");
        }
    }
}

// 采购审批系统
class PurchaseApprovalSystem {
    private Approver chain;

    public PurchaseApprovalSystem() {
        // 创建审批链
        Approver director = new Director("张主任");
        Approver vicePresident = new VicePresident("李副董事长");
        Approver president = new President("王董事长");
        Approver board = new BoardOfDirectors("董事会");

        // 设置责任链
        director.setSuccessor(vicePresident);
        vicePresident.setSuccessor(president);
        president.setSuccessor(board);

        chain = director;
    }

    public void submitRequest(PurchaseRequest request) {
        System.out.println("提交采购申请：" + request.getPurpose() + "，金额：" + request.getAmount() + "元");
        chain.processRequest(request);
        System.out.println("----------------------");
    }
}

// 测试类
public class SCMApprovalSystem {
    public static void main(String[] args) {
        PurchaseApprovalSystem system = new PurchaseApprovalSystem();

        // 测试不同金额的采购单
        system.submitRequest(new PurchaseRequest(30000, "购买办公设备"));
        system.submitRequest(new PurchaseRequest(80000, "采购原材料"));
        system.submitRequest(new PurchaseRequest(250000, "购买生产设备"));
        system.submitRequest(new PurchaseRequest(600000, "建设新厂房"));
        system.submitRequest(new PurchaseRequest(4500, "购买办公用品"));
    }
}